/*** manager ***/
package comm;

import db.DataBase;
import java.util.Arrays;
import java.util.HashMap;

public class Sales {

  private static String[][] data;
  private static String[] headers;
  private static String[][] req;
  public static final int HEADER_ID = 0;
  public static final int HEADER_PRODUCT = 1;
  public static final int HEADER_AMOUNT = 2;
  public static final int HEADER_TOTAL = 3;
  public static final int HEADER_DATE = 4;
  public static final int HEADER_REQ_AMOUNT = 2;
  public static final int HEADER_REQ_DATE = 3;

  /** implementar *********************************************************/
  private static void setData() {
    data = DataBase.readData(DataBase.SALES);
    headers = new String[]{
              "ID", "Produto", "Quantia vendida", "Total da venda", "Data"
            };
    req = DataBase.readData(DataBase.REQUISITIONS);
  }

  public static String[] getHeaders() {
    return headers;
  }

  public static String[][] getData() {
    setData();
    return data;
  }

  public static String[] getData(int row) {
    setData();
    return data[row];
  }

  private static String getID() {
    String id = "";
      id = Integer.toString(((int) (Math.random() * 10000)) + 1);
      for (int i = 0; i < data.length; i++) {
        if (data[i][0].equals(id)) {
          return null;
        }
      }
      return id;
  }

  public static String getValidID() {
    setData();
    String res = null;
    while(res == null) {
      res = getID();
    }
    return res;
  }

  /**
   * Retorna os objetos para criação do grafico, os objetos estao na ordem:
   *  0 - String data
   *  1 - Integer total absoluto
   *  2 - Float total em reais
   * @return os dados do grafico
   */
  public static Object[][] getSalesChartData() {
    setData();
    HashMap mapA = new HashMap();
    HashMap mapT = new HashMap();

    String date;
    Integer amount;
    Float total;

    for (String[] row : data) {
      date = row[HEADER_DATE];
      date = date.indexOf(' ') == -1 ? date : date.substring(0, date.indexOf(' '));
      amount = Integer.valueOf(row[HEADER_AMOUNT]);
      total = Float.valueOf(row[HEADER_TOTAL].replace("R$ ", ""));
      if (mapA.containsKey(date)) {
        amount += (Integer) mapA.get(date);
        total += (Float) mapT.get(date);
      }
      if (amount == null) {
        System.out.println(date);
      }
      mapA.put(date, amount);
      mapT.put(date, total);
    }
    Object[] keys = mapA.keySet().toArray();
    Arrays.sort(keys);

    Object[][] res = new Object[keys.length][3];
    for (int i = 0; i < keys.length; i++) {
      res[i][0] = keys[i];
      res[i][1] = mapA.get(keys[i]);
      res[i][2] = mapT.get(keys[i]);
    }
    return res;
  }

  public static Object[][] getReqChartData() {
    setData();
    HashMap map = new HashMap();

    String date;
    Integer amount;

    for (String[] row : req) {
      date = row[HEADER_REQ_DATE];
      date = date.indexOf(' ') == -1 ? date : date.substring(0, date.indexOf(' '));
      amount = Integer.valueOf(row[HEADER_REQ_AMOUNT]);
      if (map.containsKey(date)) {
        amount += (Integer) map.get(date);
      }
      if (amount == null) {
        System.out.println(date);
      }
      map.put(date, amount);
    }
    Object[] keys = map.keySet().toArray();
    Arrays.sort(keys);

    Object[][] res = new Object[keys.length][2];
    for (int i = 0; i < keys.length; i++) {
      res[i][0] = keys[i];
      res[i][1] = map.get(keys[i]);
    }
    return res;
  }

  public static int getAmountByDate(String date) {
    setData();
    date = date.indexOf(' ') == -1 ? date : date.substring(0, date.indexOf(' '));
    int res = 0;
    for (String[] d : data) {
      if (d[HEADER_DATE].startsWith(date)) {
        res += Integer.parseInt(d[HEADER_AMOUNT]);
      }
    }
    return res;
  }

  public static float getTotalByDate(String date) {
    setData();
    date = date.indexOf(' ') == -1 ? date : date.substring(0, date.indexOf(' '));
    float res = 0.0f;
    for (String[] d : data) {
      if (d[HEADER_DATE].startsWith(date)) {
        String aux = d[HEADER_TOTAL].replace("R$ ", "");
        aux = aux.replace(',', '.');
        res += Float.parseFloat(aux);
      }
    }
    return res;
  }
}
